package com.ktjiaoyu.crm.shiro.ShiroTester;

import org.junit.Test;

import javax.security.auth.Subject;

/**
 * @Author：南小楠
 * @Date: 2021/12/13 9:54
 */
public class ShiroTester {
    private System SecurityUtils;

    @Test
    public <DefaultSecurityManager> void testShiro() {
        //1、创建Realm(安全数据源)
        // 通过shiro.ini配置文件创建Realm
        IniRealm realm = new IniRealm("classpath:shiro.ini");
        // 2、配置SecurityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        // 注入创建的Realm(安全数据源)
        securityManager.setRealm(realm);
        SecurityUtils.setSecurityManager(securityManager);
        // 3、操作Subject，进行认证
        Subject subject = SecurityUtils.getSubject();
        // 封装一个令牌
        UsernamePasswordToken token = new UsernamePasswordToken("admin", "123456");
        try {
            subject.login(token);//登录，即认证
        } catch (AuthenticationException e) {
            System.out.println("认证异常：");
            e.printStackTrace();
        }
    }
    System.out.println("是否认证通过："+subject.isAuthenticated());
   System.out.println("身份信息："+subject.getPrincipal());
}

